package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.labdata.domain.LabGasCylinderNumber;
import com.ruoyi.labdata.service.ILabGasCylinderNumberService;
import com.ruoyi.system.domain.ProductGasCylinder;
import com.ruoyi.system.mapper.ProductGasCylinderMapper;
import com.ruoyi.system.service.IProductGasCylinderService;

/**
 * 气瓶Service业务层处理
 * 
 * @author malang
 * @date 2022-02-02
 */
@Service
public class ProductGasCylinderServiceImpl implements IProductGasCylinderService {
	@Autowired
	private ProductGasCylinderMapper productGasCylinderMapper;
	@Autowired
	private ILabGasCylinderNumberService labGasCylinderNumberService;

	/**
	 * 查询气瓶
	 * 
	 * @param id
	 *            气瓶ID
	 * @return 气瓶
	 */
	@Override
	public ProductGasCylinder selectProductGasCylinderById(Long id) {
		return productGasCylinderMapper.selectProductGasCylinderById(id);
	}

	/**
	 * 查询气瓶列表
	 * 
	 * @param productGasCylinder
	 *            气瓶
	 * @return 气瓶
	 */
	@Override
	public List<ProductGasCylinder> selectProductGasCylinderList(ProductGasCylinder productGasCylinder) {
		return productGasCylinderMapper.selectProductGasCylinderList(productGasCylinder);
	}

	/**
	 * 新增气瓶
	 * 
	 * @param productGasCylinder
	 *            气瓶
	 * @return 结果
	 */
	@Override
	public int insertProductGasCylinder(ProductGasCylinder productGasCylinder) {
		productGasCylinder.setStatus("0");
		LabGasCylinderNumber labGasCylinderNumber = new LabGasCylinderNumber();
		labGasCylinderNumber.setCylindersTypeId(productGasCylinder.getProductGasType());
		labGasCylinderNumber.setLabId(productGasCylinder.getDeptId());
		List<LabGasCylinderNumber> list = labGasCylinderNumberService.selectLabGasCylinderNumberList(labGasCylinderNumber);
		if(list.size()==0){
			throw new RuntimeException("当前实验室尚未设置该种类型气瓶允许存放的数量，请先设置.");
		}
		labGasCylinderNumber = list.get(0);
		labGasCylinderNumber.setScaninNum(1);
		int ret = labGasCylinderNumberService.scaninGasCylinderNumber(labGasCylinderNumber);
		if(ret==0){
			throw new RuntimeException("已经超过当前实验室允许存放该种类型气瓶的数量,不能再添加.");
		}
		return productGasCylinderMapper.insertProductGasCylinder(productGasCylinder);
	}

	/**
	 * 修改气瓶
	 * 
	 * @param productGasCylinder
	 *            气瓶
	 * @return 结果
	 */
	@Override
	@Transactional
	public int updateProductGasCylinder(ProductGasCylinder productGasCylinder) {
		productGasCylinder.setStatus("1");
		int ret =  productGasCylinderMapper.updateProductGasCylinder(productGasCylinder);
		if(ret==0){
			throw new RuntimeException("气瓶已经出过库，不能再被出库！");
		}
		ProductGasCylinder query = this.selectProductGasCylinderById(productGasCylinder.getId());
		LabGasCylinderNumber labGasCylinderNumber = new LabGasCylinderNumber();
		labGasCylinderNumber.setCylindersTypeId(query.getProductGasType());
		labGasCylinderNumber.setLabId(query.getDeptId());
		List<LabGasCylinderNumber> list = labGasCylinderNumberService.selectLabGasCylinderNumberList(labGasCylinderNumber);
		if(list.size()==0){
			throw new RuntimeException("当前实验室尚未设置该种类型气瓶允许存放的数量，请先设置.");
		}
		labGasCylinderNumber.setCylinderNumberId(list.get(0).getCylinderNumberId());
		labGasCylinderNumber.setScanoutNum(1);
		ret = labGasCylinderNumberService.scanoutGasCylinderNumber(labGasCylinderNumber);
		if(ret==0){
			throw new RuntimeException("该气瓶数据已经被出库，请刷新页面数据.");
		}
		return ret;
		
	}

	/**
	 * 批量删除气瓶
	 * 
	 * @param ids
	 *            需要删除的气瓶ID
	 * @return 结果
	 */
	/*@Override
	public int deleteProductGasCylinderByIds(Long[] ids) {
		return productGasCylinderMapper.deleteProductGasCylinderByIds(ids);
	}*/

	/**
	 * 删除气瓶信息
	 * 
	 * @param id
	 *            气瓶ID
	 * @return 结果
	 */
	@Override
	@Transactional
	public int deleteProductGasCylinderById(Long id) {
		ProductGasCylinder productGasCylinder = this.selectProductGasCylinderById(id);
		if(productGasCylinder.getLeftAt()!=null){
			throw new RuntimeException("气瓶已经出库，不能被删除.");
		}
		boolean isCanDelete = DateUtils.addDays(productGasCylinder.getEnteredAt(), 15).after(new Date());
		if(!isCanDelete){
			throw new RuntimeException("气瓶进入日期已经超过15天，不能被删除.");
		}
		LabGasCylinderNumber labGasCylinderNumber = new LabGasCylinderNumber();
		labGasCylinderNumber.setCylindersTypeId(productGasCylinder.getProductGasType());
		labGasCylinderNumber.setLabId(productGasCylinder.getDeptId());
		List<LabGasCylinderNumber> list = labGasCylinderNumberService.selectLabGasCylinderNumberList(labGasCylinderNumber);
		if(list.size()==0){
			throw new RuntimeException("当前实验室尚未设置该种类型气瓶允许存放的数量，请先设置.");
		}
		int ret = productGasCylinderMapper.deleteProductGasCylinderById(id);
		if(ret==0){
			throw new RuntimeException("该气瓶数据已经被删除，请刷新页面数据.");
		}
		labGasCylinderNumber = list.get(0);
		labGasCylinderNumber.setScanoutNum(1);
		ret = labGasCylinderNumberService.scanoutGasCylinderNumber(labGasCylinderNumber);
		if(ret==0){
			throw new RuntimeException("该气瓶数据已经被删除，请刷新页面数据.");
		}
		return ret;
	}
}
